//
//  UIImage+CAIWatermask.m
//  CAICategories
//
//  Created by 海参 on 2022/8/30.
//  Copyright © 2022 com.Chs. All rights reserved.
//

#import "UIImage+CAIWatermask.h"

@implementation WatermaskImageModel


@end

@implementation WatermaskTextModel


@end

@implementation UIImage (CAIWatermask)

+ (UIImage *)watermarkImageWithBaseDrawing:(UIImage *)originalImage watermaskElements:(NSArray *)elements{
    
    UIGraphicsBeginImageContextWithOptions(originalImage.size, YES, 1);
    [originalImage drawInRect:CGRectMake(0, 0, originalImage.size.width, originalImage.size.height)];
    [elements enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
          
        if ([obj isKindOfClass:[WatermaskImageModel class]]) {
            WatermaskImageModel *model = (WatermaskImageModel *)obj;
            [model.maskImage drawInRect:model.boundingRect];
            if (!CGRectIsEmpty(model.boundingRect)) {
                [model.maskImage drawInRect:model.boundingRect];
            }else{
                [model.maskImage drawAtPoint:CGPointMake(model.boundingRect.origin.x, model.boundingRect.origin.y)];
            }
            
        }else if ([obj isKindOfClass:[WatermaskTextModel class]]){
            WatermaskTextModel *model = (WatermaskTextModel *)obj;
            if (!CGRectIsEmpty(model.boundingRect)) {
                [model.text drawInRect:model.boundingRect withAttributes:model.attributes];
            }else{
                [model.text drawAtPoint:CGPointMake(model.boundingRect.origin.x, model.boundingRect.origin.y) withAttributes:model.attributes];
            }
        }
    }];
    
    UIImage *waterImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return waterImage;
}

@end
